package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.PageInfo;
import com.itheima.dto.OrderDto;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import com.itheima.service.ShoppingCartService;
import com.itheima.thread.TokenThreadUtils;
import com.itheima.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YangJiaHao
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Override
    public PageInfo<Order> page(LocalDateTime beginTime, LocalDateTime endTime, String number, Integer page, Integer pageSize) {
        PageInfo<Order> pageInfo = new PageInfo<>();
        Integer start = (page - 1) * pageSize;
        List<Order> orders = orderMapper.page(beginTime, endTime, number, start, pageSize);

        pageInfo.setRecords(orders);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        pageInfo.setTotal(orderMapper.countOrder());

        return pageInfo;
    }

    @Override
    public Boolean updateStatus(Integer status, String id) {
        Integer res = orderMapper.updateStatus(status, id);
        return res == 1;
    }

    @Override
    public Boolean add(OrderDto orderDto) {
        User user = orderMapper.selectUserByUserId(TokenThreadUtils.getThreadLocal().getId());
        AddressBook addressBook = orderMapper.selectAddressBookById(orderDto.getAddressBookId());
        Order order = new Order();
        BeanUtils.copyProperties(orderDto, order);
        order.setId(IdUtil.getSnowflakeNextIdStr());
        order.setNumber(IdUtil.getSnowflakeNextIdStr());
        order.setStatus(1);
        order.setUserId(TokenThreadUtils.getThreadLocal().getId());
        order.setOrderTime(LocalDateTime.now());
        order.setCheckoutTime(null);

        BigDecimal amount = new BigDecimal(0);
        List<ShoppingCart> shoppingCarts = orderMapper.selectShoppingCartByUserId(user.getId());
        for (ShoppingCart shoppingCart : shoppingCarts) {

            orderMapper.insertOrderDetail(IdUtil.getSnowflakeNextIdStr(),
                    order.getId(),shoppingCart.getName(),shoppingCart.getNumber());

            BigDecimal number = new BigDecimal(shoppingCart.getNumber());
            BigDecimal price = number.multiply(shoppingCart.getAmount());
            amount = amount.add(price);
        }
        order.setAmount(amount);

        order.setUserName(user.getName());
        order.setPhone(addressBook.getPhone());
        order.setAddress(addressBook.getDetail());
        order.setConsignee(addressBook.getConsignee());

        Integer res = orderMapper.inertOrder(order);

        shoppingCartService.clean();
        return res == 1;
    }

    @Override
    public PageInfo<OrderVo> getOrderPage(Integer page, Integer pageSize) {
        PageInfo<OrderVo> pageInfo = new PageInfo<>();
        Integer start = (page-1)*pageSize;
        List<Order> orders = orderMapper.selectOrderPage(start,pageSize);
        List<OrderVo> orderVos = new ArrayList<>();
        for (Order order : orders) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            List<OrderDetail> orderDetails = orderMapper.selectOrderDetailsByOrderId(order.getId());
            orderVo.setOrderDetails(orderDetails);
            orderVos.add(orderVo);
        }
        pageInfo.setRecords(orderVos);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        pageInfo.setPages( orders.size());
        pageInfo.setTotal((long) orders.size());
        return pageInfo;
    }
}
